/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ui.controllers;

import bl.controllers.UserJpaController;
import com.vaadin.data.Item;
import com.vaadin.data.util.BeanItem;
import entities.User;
import javax.persistence.Query;

/**
 *
 * @author Spyros
 */
public class RegistrationController {

    public BeanItem getUserDataSource() {
        return new BeanItem(new User());
    }

    public void register(Item userItem) {
        UserJpaController userJpaController = new UserJpaController();
        User user = (User) ((BeanItem) userItem).getBean();
        userJpaController.create(user);
    }

    public boolean checkIfEmailExists(String email){
        UserJpaController userJpaController = new UserJpaController();
        Query query =userJpaController.getEntityManager().createNamedQuery("User.findByEmail", User.class);
        query.setParameter("email", email);
        return !query.getResultList().isEmpty();
    }
}
